package cn.edu.jxau.util;

import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayList<T> extends List<T> {


    /**
     * 列表元素个数
     */
    private int size;

    /**
     * 列表
     */
    private T[] arr;

    /**
     * 列表初始容量
     */
    private static final int INIT_CAPACITY = 10;

    public ArrayList() {
        arr = (T[]) new Object[INIT_CAPACITY];
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            private int cursor = 0;

            @Override
            public boolean hasNext() {
                return cursor < size;
            }

            @Override
            public T next() {
                if (!hasNext()) {
                    throw new NoSuchElementException("迭代器所处位置无元素");
                }
                return arr[cursor++];
            }
        };
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(int i, T item) {

        // 参数校验 //
        if (i < 0 || i > size) {
            throw new IllegalArgumentException(String.format("i=%d，无效索引值", i));
        }

        // 插入元素 //
        if (size == arr.length) { // 数组已满，扩容
            resize(arr.length * 2);
        }
        for (int p = size; p > i; p--) { // 移动数组
            arr[p] = arr[p - 1];
        }
        arr[i] = item;
        size++;
    }

    @Override
    public void remove(T item) {
        
        int i = indexOf(item);
        if(i != -1) {
            remove(i);
        }
    }

    @Override
    public T remove(int i) {

        // 参数校验 //
        if (i < 0 || i >= size) {
            throw new IllegalArgumentException(String.format("i=%d，无效索引值", i));
        }
        
        // 移除节点 //
        T item = arr[i];
        for (int p = i; p < size - 1; p++) { // 移动数组
            arr[p] = arr[p + 1];
        }
        arr[--size] = null;
        if (size * 4 < arr.length) {
            resize(arr.length / 2);
        }
        return item;
    }

    @Override
    public int indexOf(T item) {

        for (int i = 0; i < size; i++) {
            if (arr[i] == null) {
                if(item == null) {
                    return i;
                }
            } else if (arr[i].equals(item)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public T get(int i) {

        if (i < 0 || i >= size) {
            throw new IllegalArgumentException(String.format("i=%d，无效索引值", i));
        }
        return arr[i];
    }

    @Override
    public void set(int i, T item) {

        if (i < 0 || i >= size) {
            throw new IllegalArgumentException(String.format("i=%d，无效索引值", i));
        }
        arr[i] = item;
    }

    private void resize(int newCapacity) {

        if (newCapacity < INIT_CAPACITY) { // 避免缩减得太小
            return;
        }
        System.out.printf("resize to %d\n", newCapacity);
        T[] tempArr = (T[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            tempArr[i] = arr[i];
        }
        arr = tempArr;
    }

    @Override
    public String toString() {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("list size=").append(size).append("\t");
        stringBuilder.append("[");
        int i = 0;
        for (; i < size; i++) {
            stringBuilder.append(arr[i]).append(",");
        }
        if (i != 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1); // 删除最后的逗号
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
